package com.extremelylargepackage.editor;

import java.util.ArrayList;
import java.util.List;

import com.extremelylargepackage.entity.Enemy;

import com.extremelylargepackage.GameResources;
import com.extremelylargepackage.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

public class EnemySelect extends ActivityForcePortrait 
{
	private Enemy _selected;
	private int _num;
	private ListView _list;
	EnemyListItemAdapter _adapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		_selected = (Enemy) getIntent().getExtras().get("enemy");
		_num = getIntent().getExtras().getInt("num");
		setContentView(R.layout.enemy_select);
		_list = (ListView) findViewById(R.id.enemy_select);
		_list.setOnItemClickListener(new EnemyHandler());
		_list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		Button ret = (Button) findViewById(R.id.enemy_select_return);
		ret.setOnClickListener(new ReturnHandler());
		buildList();
	}
	
	private void buildList()
	{
		ArrayList<EnemyListItem> items = new ArrayList<EnemyListItem>();
		items.add(new EnemyListItem(null));
		int i = 1;
		int selection = 0;
		for(Enemy e : GameResources.getEnemies().values())
		{
			EnemyListItem item = new EnemyListItem(e);
			items.add(item);
			if(_selected != null && e.getName().equals(_selected.getName()))
				selection = i;
			i++;
		}
		_adapter = new EnemyListItemAdapter(this, items);
		_list.setAdapter(_adapter);
		_adapter.setSelected(selection);
	}
	
	@Override
	public void onBackPressed()
	{
		setResultAndFinish();
	}
	
	private void setResult()
	{
		Intent i = new Intent();
		i.putExtra("enemy", _selected);
		i.putExtra("num", _num);
		setResult(Activity.RESULT_OK, i);
	}
	
	private void setResultAndFinish()
	{
		setResult();
		finish();
	}
	
	private class EnemyHandler implements OnItemClickListener
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View v, int row, long id) 
		{
			_list.setSelection(row);
			_selected = ((EnemyListItem) _list.getItemAtPosition(row)).getEnemy();
			setResultAndFinish();
		}	
	}
	
	private class ReturnHandler implements OnClickListener
	{
		@Override
		public void onClick(View v) 
		{
			setResultAndFinish();
		}
	}
	
	private class EnemyListItem
	{
		private Enemy _enemy;
		
		public EnemyListItem(Enemy e)
		{
			_enemy = e;
		}
		
		@Override
		public String toString()
		{
			if(_enemy == null)
				return "NONE";
			return _enemy.getName();
		}
		
		public Enemy getEnemy()
		{
			return _enemy;
		}
	}
	
	private class EnemyListItemAdapter extends ArrayAdapter<EnemyListItem>
	{
		private final Context _context;
		private final List<EnemyListItem> _values;
		private int _checked = -1;
		private SparseArray<RadioButton> _rbs;
		
		public EnemyListItemAdapter(Context context, List<EnemyListItem> objects) 
		{
			super(context, R.layout.enemy_list_item, objects);
			_context = context;
			_values = objects;
			_rbs = new SparseArray<RadioButton>();
		}
		
		public void setSelected(int row)
		{
			_checked = row;
		}
		
		private void finishSelection(int position)
		{
			setSelected(position);
			for(int i = 1; i <= 8; i++)
			{
				RadioButton rb = _rbs.get(i);
				if(rb != null)
				{
					rb.setOnCheckedChangeListener(null);
					rb.setChecked(i == position);
				}
			}
			_selected = ((EnemyListItem) _list.getItemAtPosition(position)).getEnemy();
			setResultAndFinish();
		}
		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) 
		{
		    LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    View rowView = inflater.inflate(R.layout.enemy_list_item, parent, false);
		    
		    TextView textView = (TextView) rowView.findViewById(R.id.eli_label);
		    ImageView imageView = (ImageView) rowView.findViewById(R.id.eli_icon);

		    RadioButton rb = (RadioButton) rowView.findViewById(R.id.eli_select);
		    _rbs.put(position, rb);
		    if(_checked == position)
		    {
		    	rb.setChecked(true);
		    }
		    rowView.setOnClickListener(new OnClickListener()
		    {
				@Override
				public void onClick(View v) 
				{
					finishSelection(position);
				}	
		    });
		    rb.setOnCheckedChangeListener(new OnCheckedChangeListener () 
		    {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
				{
					finishSelection(position);
				}
		    });
		    EnemyListItem eli = _values.get(position);
		    Enemy e = eli.getEnemy();
		    textView.setText(eli.toString());
		    if(e != null)
		    	imageView.setImageBitmap(e.getSprite().getBitmap());
	
		    return rowView;
		  }
		
	}
}
